package com.api.shopapi.controller;

import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.*;
import com.api.shopapi.service.InstitutionStaffRelService;
import com.api.shopapi.service.NursingTypeService;
import com.api.shopapi.service.StaffService;
import com.api.shopapi.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.UUID;

//护理类型
@RequestMapping("NursingType")
@RestController
public class NursingTypeController extends BaseController {

    @Autowired
    @Qualifier("NursingTypeServiceImplOne")
    private NursingTypeService nursingTypeService;

    @Autowired
    @Qualifier("StaffServiceImplOne")
    private StaffService staffService;

    @Autowired
    @Qualifier("UserServiceImplOne")
    private UserService userService;


    @Autowired
    @Qualifier("InstitutionStaffRelServiceImplOne")
    private InstitutionStaffRelService institutionStaffRelService;


    //列表
    @RequestMapping(value = "Page", method = RequestMethod.POST)
    public MyResult Page(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            int pageIndex = jsonObject.getInteger("pageIndex");
            int pageSize = jsonObject.getInteger("pageSize");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/NursingType/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();

            PageInfo<NursingType> pageInfoNursingTypeDTO = nursingTypeService.getPageInfoNursingType(pageIndex, pageSize, institutionCode);
            return success(pageInfoNursingTypeDTO);

        } catch (Exception e) {
            insertError("/NursingType/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }

    //列表
    @RequestMapping(value = "GovPage", method = RequestMethod.POST)
    public MyResult GovPage(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            int pageIndex = jsonObject.getInteger("pageIndex");
            int pageSize = jsonObject.getInteger("pageSize");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/NursingType/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Staff staff = staffService.getByStaffCode(staffCode);

            PageInfo<NursingType> pageInfoNursingTypeDTO = nursingTypeService.pageByGovCode(pageIndex, pageSize, staff.getGovernmentCode());
            return success(pageInfoNursingTypeDTO);

        } catch (Exception e) {
            insertError("/NursingType/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }


    @RequestMapping(value = "List", method = RequestMethod.POST)
    public MyResult List(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/NursingType/List");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();
            List<NursingType> nursingTypeList = nursingTypeService.getList(institutionCode);
            return success(nursingTypeList);

        } catch (Exception e) {
            insertError("/NursingType/List", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }

    //添加
    @RequestMapping(value = "Add", method = RequestMethod.POST)
    public MyResult Add(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            String name = jsonObject.getString("name");
            int sort = jsonObject.getInteger("sort");
            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/NursingType/Add");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();
            NursingType nursingType = nursingTypeService.getByNameAndInstitutionCode(name, institutionCode);
            if (nursingType != null) {
                return failed("护理类型已存在");
            }
            nursingType = new NursingType();
            nursingType.setNursingTypeCode(UUID.randomUUID().toString());
            nursingType.setName(name);
            nursingType.setSort(sort);
            nursingType.setStaffCode(staffCode);
            nursingType.setInstitutionCode(institutionCode);

            nursingTypeService.insert(nursingType);

            Object object = new Object[]{};
            return success(object);


        } catch (Exception e) {
            insertError("/NursingType/Add", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }

    //编辑
    @RequestMapping(value = "Edit", method = RequestMethod.POST)
    public MyResult Edit(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String nursingTypeCode = jsonObject.getString("nursingTypeCode");
            String name = jsonObject.getString("name");
            int sort = jsonObject.getInteger("sort");
            int status = jsonObject.getInteger("status");
            String staffCode = jsonObject.getString("staffCode");
            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/NursingType/Edit");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();
            NursingType nursingType = nursingTypeService.getByNameAndInstitutionCode(name, institutionCode);
            if (nursingType != null) {
                if (!nursingType.getNursingTypeCode().equals(nursingTypeCode)) {
                    return failed("该护理类型已存在");
                }
            }
            nursingType = nursingTypeService.getByNursingTypeCode(nursingTypeCode);
            nursingType.setName(name);
            nursingType.setSort(sort);
            nursingType.setStatus(status);
            nursingTypeService.updateByNursingTypeCode(nursingType);
            Object object = new Object[]{};
            return success(object);

        } catch (Exception e) {
            insertError("/NursingType/Edit", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }


    }

    //护理类型查询全部（小程序）
    @RequestMapping(value = "ListAll", method = RequestMethod.POST)
    public MyResult ListAll(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String userCode = jsonObject.getString("userCode");
            PreCheck preCheck = checkSignFrontInterface(timeStr, sign, "/NursingType/ListAll");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }
            User user = userService.getByUserCode(userCode);
            String institutionCode = user.getInstitutionCode();
            List<NursingType> pageInfoNursingTypeDTO = nursingTypeService.getListNursingType(institutionCode);
            return success(pageInfoNursingTypeDTO);

        } catch (Exception e) {
            insertError("/NursingType/ListAll", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }

}
